package com.djinfo.work.activiti.controller;

import com.alibaba.fastjson.JSON;
import com.djinfo.work.activiti.pagemodel.*;
import com.djinfo.work.activiti.pagemodel.Process;
import com.djinfo.work.activiti.po.*;
import com.djinfo.work.activiti.service.LeaveService;
import com.djinfo.work.activiti.service.SystemService;
import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.entity.TbSysRoleUser;
import com.djinfo.work.entity.TbSysRoles;
import com.djinfo.work.entity.TbSysUsers;
import com.djinfo.work.util.GsonUtil;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.activiti.engine.task.Task;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/activiti")
public class ActivitiController {
    @Autowired
    RepositoryService rep;
    @Autowired
    RuntimeService runservice;
    @Autowired
    FormService formservice;
    @Autowired
    IdentityService identityservice;
    @Autowired
    LeaveService leaveservice;
    @Autowired
    TaskService taskservice;
    @Autowired
    HistoryService histiryservice;
    @Autowired
    SystemService systemservice;

    /**
     * 查看已部署的工作流
     *
     * @return
     */
    @RequestMapping("/processlist.do")
    public String process() {
        return "activiti/activiti/processlist.jsp";
    }

    /**
     * 上传并且部署工作流。
     *
     * @param uploadfile
     * @param request
     * @return
     */
    @RequestMapping("/uploadworkflow.do")
    public String fileupload(@RequestParam MultipartFile uploadfile, HttpServletRequest request) {
        try {
            MultipartFile file = uploadfile;
            String filename = file.getOriginalFilename();
            InputStream is = file.getInputStream();
            rep.createDeployment().addInputStream(filename, is).deploy();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "activiti/activiti/processlist.jsp";
    }

    @RequestMapping(value = "/getprocesslists.do")
    @ResponseBody
    public DataGrid<Process> getlist(@RequestParam("current") int current, @RequestParam("rowCount") int rowCount) {
        int firstrow = (current - 1) * rowCount;
        List<ProcessDefinition> list = rep.createProcessDefinitionQuery().listPage(firstrow, rowCount);
        int total = rep.createProcessDefinitionQuery().list().size();
        List<Process> mylist = new ArrayList<Process>();
        for (int i = 0; i < list.size(); i++) {
            Process p = new Process();
            p.setDeploymentId(list.get(i).getDeploymentId());
            p.setId(list.get(i).getId());
            p.setKey(list.get(i).getKey());
            p.setName(list.get(i).getName());
            p.setResourceName(list.get(i).getResourceName());
            p.setDiagramresourcename(list.get(i).getDiagramResourceName());
            mylist.add(p);
        }
        DataGrid<Process> grid = new DataGrid<Process>();
        grid.setCurrent(current);
        grid.setRowCount(rowCount);
        grid.setRows(mylist);
        grid.setTotal(total);
        return grid;
    }


    @RequestMapping("/showresource.do")
    public void export(@RequestParam("pdid") String pdid, @RequestParam("resource") String resource, HttpServletResponse response) throws Exception {
        ProcessDefinition def = rep.createProcessDefinitionQuery().processDefinitionId(pdid).singleResult();
        InputStream is = rep.getResourceAsStream(def.getDeploymentId(), resource);
        ServletOutputStream output = response.getOutputStream();
        IOUtils.copy(is, output);
    }

    @RequestMapping("/deletedeploy.do")
    public String deletedeploy(@RequestParam("deployid") String deployid) throws Exception {
        rep.deleteDeployment(deployid, true);
        return "activiti/activiti/processlist.jsp";
    }

    @RequestMapping("/runningprocess")
    public String task() {
        return "activiti/runningprocess";
    }

    @RequestMapping("/deptleaderaudit.do")
    public String mytask() {
        return "activiti/activiti/deptleaderaudit.html";
    }
//	public String mytask(){
//		return "activiti/activiti/deptleaderaudit.html";
//	}

    @RequestMapping("/hraudit.do")
    public String hr() {
        return "activiti/activiti/hraudit.html";
    }

    @RequestMapping("/index")
    public String my() {
        return "activiti/index.jsp";
    }

    @RequestMapping("/leaveapply.do")
//	public String leave(){
//		return "activiti/activiti/leaveapply.jsp";
//	}
    public String leave() {
        return "activiti/activiti/leaveapply.html";
    }

    @RequestMapping("/reportback.do")
    public String reprotback() {
        return "activiti/activiti/reportback.html";
    }

    @RequestMapping("/modifyapply.do")
    public String modifyapply() {
        return "activiti/activiti/modifyapply.html";
    }

    @RequestMapping("myleaves.do")
    public String myleaves() {
        return "activiti/activiti/myleaves.html";
    }

    @RequestMapping("myleaveprocess.do")
    public String myleaveprocess() {
        return "activiti/activiti/myleaveprocess.html";
    }


    @RequestMapping("/historyprocess.do")
    public String history() {
        return "activiti/activiti/historyprocess.html";
    }

    @RequestMapping(value = "/startleave.do", method = RequestMethod.POST)
    @ResponseBody
    public String start_leave(LeaveApply apply, HttpSession session) {
        String userid = (String) session.getAttribute("username");
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("applyuserid", userid);
        ProcessInstance ins = leaveservice.DostartWorkflow(apply, userid, variables);
//		System.out.println("流程id"+ins.getId()+"已启动");
        return JSON.toJSONString("sucess");
    }

    /**
     * 部门领导审批
     *
     * @param session
     * @param current
     * @param rowCount
     * @return
     */
    @RequestMapping(value = "/depttasklist.do", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public DataGrid<LeaveTask> getdepttasklist(HttpSession session, @RequestParam("current") int current, @RequestParam("rowCount") int rowCount) {
        DataGrid<LeaveTask> grid = new DataGrid<LeaveTask>();
        grid.setRowCount(rowCount);
        grid.setCurrent(current);
        grid.setTotal(0);
        grid.setRows(new ArrayList<LeaveTask>());
        //先做权限检查，对于没有部门领导审批权限的用户,直接返回空
        String userid = (String) session.getAttribute("username");
        String uid = systemservice.getUidByusername(userid);
//		TbSysUsers tbSysUsers = systemservice.getUserByid(uid);										//是否要保留
        List<TbSysRoleUser> tbSysRoleUserList = systemservice.getTbSysRoleUser(uid);
        if (tbSysRoleUserList == null)
            return grid;
        boolean flag = false;//默认没有权限
        for (int k = 0; k < tbSysRoleUserList.size(); k++) {
            TbSysRoleUser tbSysRoleUser = tbSysRoleUserList.get(k);
            TbSysRoles tbSysRoles = (TbSysRoles) systemservice.findUniqueByProperty(TbSysRoles.class, "pkRoleId", tbSysRoleUser.getRoleId());
            String roleid = tbSysRoles.getPkRoleId();
            TbSysRoles tbSysRoles1 = systemservice.getTbSysRolesbyid(roleid);
            List<Role_permission> p = systemservice.findByProperty(Role_permission.class, "roleid", tbSysRoles1.getPkRoleId());
            for (int j = 0; j < p.size(); j++) {
                Role_permission rp = p.get(j);
                Permission permission = systemservice.findUniqueByProperty(Permission.class, "pid", rp.getPermissionid());
//				Permission permission=rp.getPermission();
                if (permission.getPermissionname().equals("部门领导审批"))
                    flag = true;
                else
                    continue;
            }
        }
        if (flag == false)//无权限
        {
            return grid;
        } else {
            int firstrow = (current - 1) * rowCount;
            List<LeaveApply> results = leaveservice.getpagedepttask(userid, firstrow, rowCount);
            int totalsize = leaveservice.getalldepttask(userid);
            List<LeaveTask> tasks = new ArrayList<LeaveTask>();
            for (LeaveApply apply : results) {
                LeaveTask task = new LeaveTask();
                task.setApply_time(apply.getApply_time());
                task.setUser_id(apply.getUser_id());
                task.setEnd_time(apply.getEnd_time());
                task.setId(apply.getId());
                task.setLeave_type(apply.getLeave_type());
                task.setProcess_instance_id(apply.getProcess_instance_id());
                task.setProcessdefid(apply.getTask().getProcessDefinitionId());
                task.setReason(apply.getReason());
                task.setStart_time(apply.getStart_time());
                task.setTaskcreatetime(apply.getTask().getCreateTime());
                task.setTaskid(apply.getTask().getId());
                task.setTaskname(apply.getTask().getName());
                tasks.add(task);
            }
            grid.setRowCount(rowCount);
            grid.setCurrent(current);
            grid.setTotal(totalsize);
            grid.setRows(tasks);
            return grid;
        }
    }

    /**
     * 人事审批
     *
     * @param session
     * @param current
     * @param rowCount
     * @return
     */
    @RequestMapping(value = "/hrtasklist.do", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public DataGrid<LeaveTask> gethrtasklist(HttpSession session, @RequestParam("current") int current, @RequestParam("rowCount") int rowCount) {
        DataGrid<LeaveTask> grid = new DataGrid<LeaveTask>();
        grid.setRowCount(rowCount);
        grid.setCurrent(current);
        grid.setTotal(0);
        grid.setRows(new ArrayList<LeaveTask>());
        //先做权限检查，对于没有人事权限的用户,直接返回空
        String userid = (String) session.getAttribute("username");
        String uid = systemservice.getUidByusername(userid);
        TbSysUsers tbSysUsers = systemservice.getUserByid(uid);                                        //是否要保留
        List<TbSysRoleUser> tbSysRoleUserList = systemservice.getTbSysRoleUser(uid);
        if (tbSysRoleUserList == null)
            return grid;
        boolean flag = false;//默认没有权限
        for (int k = 0; k < tbSysRoleUserList.size(); k++) {
            TbSysRoleUser tbSysRoleUser = tbSysRoleUserList.get(k);
            TbSysRoles tbSysRoles = (TbSysRoles) systemservice.findUniqueByProperty(TbSysRoles.class, "pkRoleId", tbSysRoleUser.getRoleId());
            String roleid = tbSysRoles.getPkRoleId();
            TbSysRoles tbSysRoles1 = systemservice.getTbSysRolesbyid(roleid);
            List<Role_permission> p = systemservice.findByProperty(Role_permission.class, "roleid", tbSysRoles1.getPkRoleId());
            for (int j = 0; j < p.size(); j++) {
                Role_permission rp = p.get(j);
                Permission permission = systemservice.findUniqueByProperty(Permission.class, "pid", rp.getPermissionid());
                if (permission.getPermissionname().equals("人事审批"))
                    flag = true;
                else
                    continue;
            }
        }
        if (flag == false)//无权限
        {
            return grid;
        } else {
            int firstrow = (current - 1) * rowCount;
            List<LeaveApply> results = leaveservice.getpagehrtask(userid, firstrow, rowCount);
            int totalsize = leaveservice.getallhrtask(userid);
            List<LeaveTask> tasks = new ArrayList<LeaveTask>();
            for (LeaveApply apply : results) {
                LeaveTask task = new LeaveTask();
                task.setApply_time(apply.getApply_time());
                task.setUser_id(apply.getUser_id());
                task.setEnd_time(apply.getEnd_time());
                task.setId(apply.getId());
                task.setLeave_type(apply.getLeave_type());
                task.setProcess_instance_id(apply.getProcess_instance_id());
                task.setProcessdefid(apply.getTask().getProcessDefinitionId());
                task.setReason(apply.getReason());
                task.setStart_time(apply.getStart_time());
                task.setTaskcreatetime(apply.getTask().getCreateTime());
                task.setTaskid(apply.getTask().getId());
                task.setTaskname(apply.getTask().getName());
                tasks.add(task);
            }
            grid.setRowCount(rowCount);
            grid.setCurrent(current);
            grid.setTotal(totalsize);
            grid.setRows(tasks);
            return grid;
        }
    }

    /**
     * 销毁
     *
     * @param session
     * @param current
     * @param rowCount
     * @return
     */
    @RequestMapping(value = "/xjtasklist.do", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String getXJtasklist(HttpSession session, @RequestParam("current") int current, @RequestParam("rowCount") int rowCount) {
        int firstrow = (current - 1) * rowCount;
        String userid = (String) session.getAttribute("username");
        List<LeaveApply> results = leaveservice.getpageXJtask(userid, firstrow, rowCount);
        int totalsize = leaveservice.getallXJtask(userid);
        List<LeaveTask> tasks = new ArrayList<LeaveTask>();
        for (LeaveApply apply : results) {
            LeaveTask task = new LeaveTask();
            task.setApply_time(apply.getApply_time());
            task.setUser_id(apply.getUser_id());
            task.setEnd_time(apply.getEnd_time());
            task.setId(apply.getId());
            task.setLeave_type(apply.getLeave_type());
            task.setProcess_instance_id(apply.getProcess_instance_id());
            task.setProcessdefid(apply.getTask().getProcessDefinitionId());
            task.setReason(apply.getReason());
            task.setStart_time(apply.getStart_time());
            task.setTaskcreatetime(apply.getTask().getCreateTime());
            task.setTaskid(apply.getTask().getId());
            task.setTaskname(apply.getTask().getName());
            tasks.add(task);
        }
        DataGrid<LeaveTask> grid = new DataGrid<LeaveTask>();
        grid.setRowCount(rowCount);
        grid.setCurrent(current);
        grid.setTotal(totalsize);
        grid.setRows(tasks);
        return JSON.toJSONString(grid);
    }

    /**
     * 调整申请
     *
     * @param session
     * @param current
     * @param rowCount
     * @return
     */
    @RequestMapping(value = "/updatetasklist.do", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public String getupdatetasklist(HttpSession session, @RequestParam("current") int current, @RequestParam("rowCount") int rowCount) {
        int firstrow = (current - 1) * rowCount;
        String userid = (String) session.getAttribute("username");
        List<LeaveApply> results = leaveservice.getpageupdateapplytask(userid, firstrow, rowCount);
        int totalsize = leaveservice.getallupdateapplytask(userid);
        List<LeaveTask> tasks = new ArrayList<LeaveTask>();
        for (LeaveApply apply : results) {
            LeaveTask task = new LeaveTask();
            task.setApply_time(apply.getApply_time());
            task.setUser_id(apply.getUser_id());
            task.setEnd_time(apply.getEnd_time());
            task.setId(apply.getId());
            task.setLeave_type(apply.getLeave_type());
            task.setProcess_instance_id(apply.getProcess_instance_id());
            task.setProcessdefid(apply.getTask().getProcessDefinitionId());
            task.setReason(apply.getReason());
            task.setStart_time(apply.getStart_time());
            task.setTaskcreatetime(apply.getTask().getCreateTime());
            task.setTaskid(apply.getTask().getId());
            task.setTaskname(apply.getTask().getName());
            tasks.add(task);
        }
        DataGrid<LeaveTask> grid = new DataGrid<LeaveTask>();
        grid.setRowCount(rowCount);
        grid.setCurrent(current);
        grid.setTotal(totalsize);
        grid.setRows(tasks);
        return JSON.toJSONString(grid);
    }

    @RequestMapping(value = "/dealtask.do")
    @ResponseBody
    public AjaxJson taskdeal(@RequestParam("taskid") String taskid, HttpServletResponse response) {
        AjaxJson ajaxJson = new AjaxJson();
        HashMap<String, Object> map = new HashMap<>();
        Task task = taskservice.createTaskQuery().taskId(taskid).singleResult();
        ProcessInstance process = runservice.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
        LeaveApply leave = leaveservice.getleave(process.getBusinessKey());
        map.put("leave", leave);
        ajaxJson.setResult(map);
        return ajaxJson;
    }

    @RequestMapping(value = "/activiti/task-deptleaderaudit")
    public String url() {
        return "/activiti/task-deptleaderaudit";
    }

    @RequestMapping(value = "/task/deptcomplete.do")
    @ResponseBody
    public String deptcomplete(HttpSession session, String taskid, HttpServletRequest req) {
        String userid = (String) session.getAttribute("username");
        Map<String, Object> variables = new HashMap<String, Object>();
        String approve = req.getParameter("deptleaderapprove");
        variables.put("deptleaderapprove", approve);
        taskservice.claim(taskid, userid);
        taskservice.complete(taskid, variables);
        return JSON.toJSONString("success");
    }

    @RequestMapping(value = "/task/hrcomplete.do")
    @ResponseBody
    public String hrcomplete(HttpSession session, String taskid, HttpServletRequest req) {
        String userid = (String) session.getAttribute("username");
        Map<String, Object> variables = new HashMap<String, Object>();
        String approve = req.getParameter("hrapprove");
        variables.put("hrapprove", approve);
        taskservice.claim(taskid, userid);
        taskservice.complete(taskid, variables);
        return JSON.toJSONString("success");
    }

    @RequestMapping(value = "/task/reportcomplete.do")
    @ResponseBody
    public String reportbackcomplete(String taskid, HttpServletRequest req) {
        String realstart_time = req.getParameter("realstart_time");
        String realend_time = req.getParameter("realend_time");
        leaveservice.Docompletereportback(taskid, realstart_time, realend_time);
        return JSON.toJSONString("success");
    }

    @RequestMapping(value = "/task/updatecomplete.do")
    @ResponseBody
    public String updatecomplete(String taskid, @ModelAttribute("leave") LeaveApply leave, @RequestParam("reapply") String reapply) {
        leaveservice.updatecomplete(taskid, leave, reapply);
        return JSON.toJSONString("success");
    }

    @RequestMapping("involvedprocess.do")//参与的正在运行的请假流程
    @ResponseBody
    public DataGrid<RunningProcess> allexeution(HttpSession session, @RequestParam("current") int current, @RequestParam("rowCount") int rowCount) {
        int firstrow = (current - 1) * rowCount;
        String userid = (String) session.getAttribute("username");
        ProcessInstanceQuery query = runservice.createProcessInstanceQuery();
        int total = (int) query.count();
        List<ProcessInstance> a = query.processDefinitionKey("leave").involvedUser(userid).listPage(firstrow, rowCount);
        List<RunningProcess> list = new ArrayList<RunningProcess>();
        for (ProcessInstance p : a) {
            RunningProcess process = new RunningProcess();
            process.setActivityid(p.getActivityId());
            process.setBusinesskey(p.getBusinessKey());
            process.setExecutionid(p.getId());
            process.setProcessInstanceid(p.getProcessInstanceId());
            list.add(process);
        }
        DataGrid<RunningProcess> grid = new DataGrid<RunningProcess>();
        grid.setCurrent(current);
        grid.setRowCount(rowCount);
        grid.setTotal(total);
        grid.setRows(list);
        return grid;
    }

    @RequestMapping("/getfinishprocess.do")
    @ResponseBody
    public DataGrid<HistoryProcess> getHistory(HttpSession session, @RequestParam("current") int current, @RequestParam("rowCount") int rowCount) {
        String userid = (String) session.getAttribute("username");
        HistoricProcessInstanceQuery process = histiryservice.createHistoricProcessInstanceQuery().processDefinitionKey("leave").startedBy(userid).finished();
        int total = (int) process.count();
        int firstrow = (current - 1) * rowCount;
        List<HistoricProcessInstance> info = process.listPage(firstrow, rowCount);
        List<HistoryProcess> list = new ArrayList<HistoryProcess>();
        for (HistoricProcessInstance history : info) {
            HistoryProcess his = new HistoryProcess();
            String bussinesskey = history.getBusinessKey();
            LeaveApply apply = leaveservice.getleave(bussinesskey);
            his.setLeaveapply(apply);
            his.setBusinessKey(bussinesskey);
            his.setProcessDefinitionId(history.getProcessDefinitionId());
            list.add(his);
        }
        DataGrid<HistoryProcess> grid = new DataGrid<HistoryProcess>();
        grid.setCurrent(current);
        grid.setRowCount(rowCount);
        grid.setTotal(total);
        grid.setRows(list);
        return grid;
    }


    @RequestMapping("/processinfo.do")
    @ResponseBody
    public List<HistoricActivityInstance> processinfo(@RequestParam("instanceid") String instanceid) {
        List<HistoricActivityInstance> his = histiryservice.createHistoricActivityInstanceQuery().processInstanceId(instanceid).orderByHistoricActivityInstanceStartTime().asc().list();
        return his;
    }

    @RequestMapping("/processhis")
    @ResponseBody
    public List<HistoricActivityInstance> processhis(@RequestParam("ywh") String ywh) {
        String instanceid = histiryservice.createHistoricProcessInstanceQuery().processDefinitionKey("purchase").processInstanceBusinessKey(ywh).singleResult().getId();
        System.out.println(instanceid);
        List<HistoricActivityInstance> his = histiryservice.createHistoricActivityInstanceQuery().processInstanceId(instanceid).orderByHistoricActivityInstanceStartTime().asc().list();
        return his;
    }


    @RequestMapping("traceprocess.do")
    public void traceprocess(String executionid, HttpServletResponse response) throws Exception {
        ProcessInstance process = runservice.createProcessInstanceQuery().processInstanceId(executionid).singleResult();
        BpmnModel bpmnmodel = rep.getBpmnModel(process.getProcessDefinitionId());
        List<String> activeActivityIds = runservice.getActiveActivityIds(executionid);
        DefaultProcessDiagramGenerator gen = new DefaultProcessDiagramGenerator();
        // 获得历史活动记录实体（通过启动时间正序排序，不然有的线可以绘制不出来）
        List<HistoricActivityInstance> historicActivityInstances = histiryservice
                .createHistoricActivityInstanceQuery().executionId(executionid)
                .orderByHistoricActivityInstanceStartTime().asc().list();
        // 计算活动线
        List<String> highLightedFlows = leaveservice.getHighLightedFlows(
                (ProcessDefinitionEntity) ((RepositoryServiceImpl) rep)
                        .getDeployedProcessDefinition(process.getProcessDefinitionId()),
                historicActivityInstances);

        InputStream in = gen.generateDiagram(bpmnmodel, "png", activeActivityIds, highLightedFlows, "宋体", "宋体", null, 1.0);
        //InputStream in=gen.generateDiagram(bpmnmodel, "png", activeActivityIds);
        ServletOutputStream output = response.getOutputStream();
        IOUtils.copy(in, output);
    }


    @RequestMapping("setupprocess.do")
    @ResponseBody
    public DataGrid<RunningProcess> setupprocess(HttpSession session, @RequestParam("current") int current, @RequestParam("rowCount") int rowCount) {
        int firstrow = (current - 1) * rowCount;
        String userid = (String) session.getAttribute("username");
        ProcessInstanceQuery query = runservice.createProcessInstanceQuery();
        int total = (int) query.count();
        List<ProcessInstance> a = query.processDefinitionKey("leave").involvedUser(userid).listPage(firstrow, rowCount);
        List<RunningProcess> list = new ArrayList<RunningProcess>();
        for (ProcessInstance p : a) {
            RunningProcess process = new RunningProcess();
            process.setActivityid(p.getActivityId());
            process.setBusinesskey(p.getBusinessKey());
            process.setExecutionid(p.getId());
            process.setProcessInstanceid(p.getProcessInstanceId());
            LeaveApply l = leaveservice.getleave(p.getBusinessKey());
            if (l.getUser_id().equals(userid))
                list.add(process);
            else
                continue;
        }
        DataGrid<RunningProcess> grid = new DataGrid<RunningProcess>();
        grid.setCurrent(current);
        grid.setRowCount(rowCount);
        grid.setTotal(total);
        grid.setRows(list);
        return grid;
    }

}
